<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Java的OJ编程模板 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/6.8c2ea393.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/149.ffc835b5.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/20.b5af7fad.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/26.74ba4989.js"><link rel="prefetch" href="/assets/js/27.13d60edd.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/42.76f52d62.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/57.a9caec0d.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/72.e5467385.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link router-link-active">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link router-link-active">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>Java的OJ编程模板</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#目录" class="sidebar-link">目录</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#⭐️java在oj的万能模板" class="sidebar-link">⭐️Java在OJ的万能模板</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅模块1-思考对比" class="sidebar-link">✅模块1.思考对比</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#🤔int和integer有什么区别？" class="sidebar-link">🤔int和Integer有什么区别？</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#🤔string、stringbuffer、stringbuilder有什么区别？" class="sidebar-link">🤔String、StringBuffer、StringBuilder有什么区别？</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#🤔对比vector、arraylist和linkedlist" class="sidebar-link">🤔对比Vector、ArrayList和LinkedList</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#🤔对比hashtable、hashmap、treemap有什么不同？" class="sidebar-link">🤔对比Hashtable、HashMap、TreeMap有什么不同？</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#🌱模块2-api记忆" class="sidebar-link">🌱模块2.API记忆</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅01-字符串stringbuilder和string" class="sidebar-link">✅01.字符串StringBuilder和String</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅02-stack" class="sidebar-link">✅02.Stack</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅03-queue" class="sidebar-link">✅03.Queue</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#⭐️对比记忆" class="sidebar-link">⭐️对比记忆</a></li></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅04-哈希表hashmap" class="sidebar-link">✅04.哈希表HashMap</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅05-哈希集合hashset" class="sidebar-link">✅05.哈希集合HashSet</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅06-动态数组arraylist" class="sidebar-link">✅06.动态数组ArrayList</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅07-双向链表linkedlist" class="sidebar-link">✅07.双向链表LinkedList</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#✅08-优先队列-大顶堆" class="sidebar-link">✅08.优先队列/大顶堆</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#优先队列基础" class="sidebar-link">优先队列基础</a></li></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#🌱java-se-常用类-目录" class="sidebar-link">🌱Java SE 常用类-目录</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#_1-java-lang包" class="sidebar-link">1.java.lang包</a></li><li class="sidebar-sub-header"><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#_3-java-util包" class="sidebar-link">3.java.util包</a></li></ul></li><li><a href="/01.Language/Java/Part02.Java%E6%B3%9B%E5%9E%8B.html#♻️参考资料" class="sidebar-link">♻️参考资料</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="java的oj编程模板"><a href="#java的oj编程模板" class="header-anchor">#</a> Java的OJ编程模板</h1> <ul><li><strong>Java 1.5发行版本中增加了泛型(Generic)</strong>。
<ul><li>参考自：《Effietive Java》P97</li></ul></li> <li>⚡️OJ中Java常用数据结构</li></ul> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>&lt;font style=&quot;background:yellow&quot;&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="目录"><a href="#目录" class="header-anchor">#</a> 目录</h2> <p>[TOC]</p> <h2 id="⭐️java在oj的万能模板"><a href="#⭐️java在oj的万能模板" class="header-anchor">#</a> ⭐️Java在OJ的万能模板</h2> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span>	<span class="token comment">//Scanner等，还有各种泛型数据结构</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>lang<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span>	<span class="token comment">//字符串</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>math<span class="token punctuation">.</span></span><span class="token operator">*</span><span class="token punctuation">;</span>	<span class="token comment">//大整型+大浮点型</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><img src="/assets/img/Java内置的数据结构.b6e0df56.png"> <h2 id="✅模块1-思考对比"><a href="#✅模块1-思考对比" class="header-anchor">#</a> ✅模块1.思考对比</h2> <h2 id="🤔int和integer有什么区别？"><a href="#🤔int和integer有什么区别？" class="header-anchor">#</a> 🤔int和Integer有什么区别？</h2> <ul><li><code>int</code>是我们常说的整形数字，是Java的8个原始数据类型之一。Java语言虽然号称一切都是对象， 但<strong>原始数据类型</strong>是例外。</li> <li><code>Integer</code>是int对应的包装类，它有一个int类型的字段存储数据，并且提供了基本操作，比如数学运算、int和字符串之间转换等。在<strong>Java 5</strong>中，引入了自动装箱和自动拆箱功能 （<code>boxing/unboxing</code>），Java可以根据上下文，自动进行转换，极大地简化了相关编程。关于Integer的值缓存，这涉及Java 5中另一个改进。构建Integer对象的传统方式是直接调用构造器(<code>constructor</code>)，直接new一个对象。但是根据实践，我们发现大部分数据操作都是集中在有 限的、较小的数值范围，因而，在Java 5中新增了静态工厂方法valueOf，在调用它的时候会利用一个缓存机制，带来了明显的性能改进。按照Javadoc，这个值默认缓存 是-128到127之间。</li></ul> <h2 id="🤔string、stringbuffer、stringbuilder有什么区别？"><a href="#🤔string、stringbuffer、stringbuilder有什么区别？" class="header-anchor">#</a> 🤔String、StringBuffer、StringBuilder有什么区别？</h2> <ul><li><p><code>String</code>是Java语言非常基础和重要的类，提供了构造和管理字符串的各种基本逻辑。它是典型的<strong>Immutable类</strong>，被声明成为final class，所有属性也都是fnal的。也由于它的不可变性，类似拼接、裁剪字符串等动作，都会产生新的String对象。由于字符串操作的普遍性，所以相关操作的效率往往对应用性能有明显影响。</p></li> <li><p><code>StringBufer</code>是为解决上面提到拼接产生太多中间对象的问题而提供的一个类。StringBufer本质是一个<strong>线程安全</strong>的可修改字符序列，它保证了线程安全，也随之带来了<strong>额外的性能开销</strong>，所以除非有线程安全的需要，不然还是推荐使用它的<strong>后继者</strong>，也就是StringBuilder。</p></li> <li><p><code>StringBuilder</code>是<code>Java 1.5</code>中新增的，在能力上和StringBufer没有本质区别，但是它<strong>去掉了线程安全</strong>的部分，有效<strong>减小了开销</strong>，是绝大部分情况下进行<font style="background:yellow;">字符串拼接的首选</font></p></li></ul> <h2 id="🤔对比vector、arraylist和linkedlist"><a href="#🤔对比vector、arraylist和linkedlist" class="header-anchor">#</a> 🤔对比<code>Vector</code>、<code>ArrayList</code>和<code>LinkedList</code></h2> <ul><li>这三者都是实现集合框架中的<strong>List</strong>，也就是所谓的有序集合，因此具体功能也比较近似，比如都提供按照位置进行定位、添加或者删除的操作，都提供迭代器以遍历其内容等。但因为具体的设计区别，在行为、性能、线程安全等方面，表现又有很大不同</li> <li><code>Vector</code>是<strong>Java早期</strong>提供的<strong>线程安全</strong>的动态数组，如果不需要线程安全，并不建议选择，毕竟同步是有额外开销的。Vector内部是使用对象数组来保存数据，可以根据需要自动的增加容量，当数组已满时，会创建新的数组，并拷贝原有数组数据。『扩容逻辑：Vector在扩容时会提高1倍』</li> <li><code>ArrayList</code>是应用更加广泛的<font style="background:yellow;">动态数组</font>实现，它本身<strong>不是线程安全</strong>的，所以性能要好很多。与Vector近似，ArrayList也是可以根据需要调整容量，不过两者的调整逻辑有所区 别，『扩容逻辑：ArrayList则是增加50%』<code>ArrayList</code>列表底层是用『数组』实现的</li> <li><code>LinkedList</code>顾名思义是Java提供的<font style="background:yellow;">双向链表</font>，所以它不需要像上面两种那样调整容量，它也<strong>不是线程安全</strong>的。<code>LinkedList</code>底层是用『双链表』实现的。</li></ul> <h2 id="🤔对比hashtable、hashmap、treemap有什么不同？"><a href="#🤔对比hashtable、hashmap、treemap有什么不同？" class="header-anchor">#</a> 🤔对比<code>Hashtable</code>、<code>HashMap</code>、<code>TreeMap</code>有什么不同？</h2> <ul><li><a href="https://segmentfault.com/a/1190000019945054" target="_blank" rel="noopener noreferrer">java中HashMap和Hashtable的区别<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li> <li>Hashtable、HashMap、TreeMap都是最常见的一些Map实现，是以键值对的形式存储和操作数据的容器类型。</li> <li><code>Hashtable</code>是<strong>早期Java类库</strong>提供的一个哈希表实现，<strong>本身是同步的</strong>，不支持null键和值，由于同步导致的性能开销，所以已经<strong>很少被推荐使用</strong></li> <li><code>HashMap</code>是应用<strong>更加广泛</strong>的哈希表实现，行为上大致上与HashTable一致，主要区别在于HashMap<strong>不是同步的</strong>，支持null键和值等。通常情况下，HashMap进行put或者get操 作，可以达到常数时间的性能，所以它是绝大部分利用键值对存取场景的首选。</li> <li><code>TreeMap</code>则是<strong>基于红黑树</strong>的一种提供顺序访问的Map，和HashMap不同，它的get、put、remove之类操作都是O（log(n)）的时间复杂度，具体顺序可以由指定 的Comparator来决定，或者根据键的自然顺序来判断。</li></ul> <h2 id="🌱模块2-api记忆"><a href="#🌱模块2-api记忆" class="header-anchor">#</a> 🌱模块2.API记忆</h2> <h2 id="✅01-字符串stringbuilder和string"><a href="#✅01-字符串stringbuilder和string" class="header-anchor">#</a> ✅01.字符串<code>StringBuilder</code>和<code>String</code></h2> <h4 id="string基本操作"><a href="#string基本操作" class="header-anchor">#</a> String基本操作</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">String</span> str<span class="token operator">=</span><span class="token string">&quot;HACV&quot;</span><span class="token punctuation">;</span>

<span class="token comment">//注意，Java的字符串处理起来比C++麻烦，因为它不支持用[]直接访问其中的字符，而且不能直接修改</span>
<span class="token comment">//需要转换为char[] 类型后才能修改</span>
<span class="token comment">//获取str[2];</span>
<span class="token keyword">char</span> c<span class="token operator">=</span>str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//Java的字符串不能直接修改，要用toCharArray转化为『char[]』类型的数组后进行修改，然后转换回『String』类型</span>
<span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> demo<span class="token operator">=</span>str<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
demo<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">'a'</span><span class="token punctuation">;</span>

<span class="token class-name">String</span> strTemp<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span> demo <span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span> strTemp <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h4 id="string的其他操作"><a href="#string的其他操作" class="header-anchor">#</a> String的其他操作</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//注意。一定要用equals方法判断字符串是否相同</span>
<span class="token comment">//不要用==比较，否则可能出现不易察觉的bug</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> s1<span class="token punctuation">.</span><span class="token function">euqals</span><span class="token punctuation">(</span>s2<span class="token punctuation">)</span> <span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//s1和s2相同</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{</span>
    <span class="token comment">//s1和s2不相同</span>
<span class="token punctuation">}</span>

<span class="token comment">//字符串可以用『加号』进行拼接</span>
<span class="token class-name">String</span> s3<span class="token operator">=</span>s1<span class="token operator">+</span><span class="token string">&quot;!!!&quot;</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s3<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h4 id="第2种"><a href="#第2种" class="header-anchor">#</a> 第2种</h4> <ul><li>虽然String支持用<code>+</code>进行拼接，但是效率并不高，不建议在for循环中使用</li> <li>如果需要进行频繁的字符串拼接，推荐使用<code>StringBuilder</code></li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">StringBuilder</span> sb<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">char</span> c<span class="token operator">=</span><span class="token string">'a'</span><span class="token punctuation">;</span> c<span class="token operator">&lt;</span><span class="token string">'f'</span><span class="token punctuation">;</span> c<span class="token operator">++</span> <span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    sb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">//append方法支持拼接字符、字符串、数字等类型</span>
sb<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">'g'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string">&quot;hhh&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token class-name">String</span> res<span class="token operator">=</span>sb<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h2 id="✅02-stack"><a href="#✅02-stack" class="header-anchor">#</a> ✅02.Stack</h2> <h4 id="栈的基础"><a href="#栈的基础" class="header-anchor">#</a> 栈的基础</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Stack</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> s<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Stack</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="栈的常用函数"><a href="#栈的常用函数" class="header-anchor">#</a> 栈的常用函数</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">boolean</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//判断Stack是否空</span>
<span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>				<span class="token comment">//返回Stack中元素个数</span>
<span class="token class-name">ElementType</span> <span class="token function">push</span><span class="token punctuation">(</span> <span class="token class-name">ElementType</span> item <span class="token punctuation">)</span><span class="token punctuation">;</span>	 <span class="token comment">//把元素压入栈顶</span>
<span class="token class-name">ElementType</span> <span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//返回栈顶元素『和C++ STL不一样』</span>
<span class="token class-name">ElementType</span> <span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//删除并且返回栈顶元素『和C++ STL不一样』</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="✅03-queue"><a href="#✅03-queue" class="header-anchor">#</a> ✅03.Queue</h2> <h4 id="队列的基础"><a href="#队列的基础" class="header-anchor">#</a> 队列的基础</h4> <p>注意：<font style="background:yellow;">注意这个初始化</font></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Queue</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> qq<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//注意！『前面是Queue，后面是LinkedList』</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h4 id="队列的常用函数"><a href="#队列的常用函数" class="header-anchor">#</a> 队列的常用函数</h4> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">boolean</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//判断Queue是否空</span>
<span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>				<span class="token comment">//返回Queue中元素个数</span>
<span class="token class-name">ElementType</span> <span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//返回队头的元素『和C++ STL不一样』</span>
<span class="token class-name">ElementType</span> <span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//删除并返回队头的元素『和C++ STL不一样』</span>
<span class="token keyword">boolean</span> <span class="token function">offer</span><span class="token punctuation">(</span> <span class="token class-name">ElementType</span> val <span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//将元素val插入队尾</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="⭐️对比记忆"><a href="#⭐️对比记忆" class="header-anchor">#</a> ⭐️对比记忆</h3> <blockquote><p>对比记忆1：</p></blockquote> <ul><li><code>C++ STL</code>中『push，压入』『pop，弹出』</li> <li>Java的队列中『offer，插入队尾』『poll，删除并返回队头的元素』</li> <li>Git中『push，提交去远程』『pull，从远程拉取到本地』</li></ul> <blockquote><p>对比记忆2：</p></blockquote> <ul><li>C++ STL中返回『<strong>栈顶</strong>』元素，用『top』</li> <li>C++ STL中返回『<strong>队列</strong>头』元素，用『front』，返回队尾用『back』</li> <li>Java中『<strong>栈顶</strong>』元素/『<strong>队列</strong>头』元素，全都用的『peek』</li></ul> <h2 id="✅04-哈希表hashmap"><a href="#✅04-哈希表hashmap" class="header-anchor">#</a> ✅04.哈希表HashMap</h2> <blockquote><p>HashMap『记忆方式，C++ STL中也常用map系列来做哈希』</p></blockquote> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> mp<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//整数映射到字符串的哈希表</span>
<span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">&gt;</span> mp<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//字符串映射到数组的哈希表</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>常用函数</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">boolean</span> <span class="token function">containsKey</span><span class="token punctuation">(</span> <span class="token class-name">Object</span> key <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//判断哈希表中是否存在键『key』</span>
val <span class="token function">get</span><span class="token punctuation">(</span> <span class="token class-name">Objevt</span> key <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获得键key对应的值val，如果key不存在，则返回null</span>
val <span class="token function">put</span><span class="token punctuation">(</span> <span class="token class-name">Key</span> key<span class="token punctuation">,</span> <span class="token class-name">Val</span> value <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//键值对存入哈希表</span>
val <span class="token function">remove</span><span class="token punctuation">(</span> <span class="token class-name">Object</span> key <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//如果key存在，删除key并且返回对应的值</span>
val <span class="token function">getOrDefault</span><span class="token punctuation">(</span> <span class="token class-name">Object</span> key<span class="token punctuation">,</span> val defaultValue <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获得key的值，如果key不存在，则返回defaultValue</span>

<span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">K</span><span class="token punctuation">&gt;</span></span> <span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获得哈希表中的所有key</span>

<span class="token comment">//如果key不存在，则将键值对key和value存入哈希表</span>
<span class="token comment">//如果key存在，则什么都不做</span>
val <span class="token function">putIfAbsent</span><span class="token punctuation">(</span> <span class="token class-name">Key</span> key<span class="token punctuation">,</span> <span class="token class-name">Val</span> value <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h2 id="✅05-哈希集合hashset"><a href="#✅05-哈希集合hashset" class="header-anchor">#</a> ✅05.哈希集合HashSet</h2> <blockquote><p>HashSet</p></blockquote> <p>注意：<font style="background:yellow;">注意这个初始化</font></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> set<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//新建一个存储String的哈希集合</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>常用函数</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">boolean</span> <span class="token function">add</span><span class="token punctuation">(</span> <span class="token class-name">ElementType</span> e <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//如果e不存在，则将e添加到哈希集合</span>
<span class="token keyword">boolean</span> <span class="token function">contains</span><span class="token punctuation">(</span> <span class="token class-name">Object</span> <span class="token class-name">O</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//判断元素O是否存在于哈希集合中</span>
<span class="token keyword">boolean</span> <span class="token function">remove</span><span class="token punctuation">(</span> <span class="token class-name">Object</span> o <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//如果元素o存在，则删除元素o</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="✅06-动态数组arraylist"><a href="#✅06-动态数组arraylist" class="header-anchor">#</a> ✅06.动态数组ArrayList</h2> <ul><li>类似C++的vector容器，ArrayList相当于把Java内置的数组类型做了包装</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> str<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//初始化一个存储int类型数据的动态数组</span>
<span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span>  str<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//初始化一个存储String类型数据的动态数组</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>常见函数</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">boolean</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//判断数组是否为空</span>
<span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//返回数组中元素的格式</span>
<span class="token class-name">ElementType</span> <span class="token function">get</span><span class="token punctuation">(</span> <span class="token keyword">int</span> index <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//返回索引index的元素</span>
<span class="token keyword">boolean</span> <span class="token function">add</span><span class="token punctuation">(</span> <span class="token class-name">ElementType</span> e <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在数组尾部添加元素e</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="✅07-双向链表linkedlist"><a href="#✅07-双向链表linkedlist" class="header-anchor">#</a> ✅07.双向链表LinkedList</h2> <ul><li>LinkedList底层是双向链表实现的</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">LinkedList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">&gt;</span></span> str<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//初始化一个存储int类型数据的双链表</span>
<span class="token class-name">LinkedList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span>  str<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">LinkedList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//初始化一个存储String类型数据的双链表</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>常用函数</li></ul> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">boolean</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//判断链表是否为空</span>
<span class="token keyword">int</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//返回链表中元素的个数</span>
<span class="token keyword">boolean</span> <span class="token function">contains</span><span class="token punctuation">(</span> <span class="token class-name">Object</span> o <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//判断链表中是否存在元素o</span>

<span class="token keyword">boolean</span> <span class="token function">add</span><span class="token punctuation">(</span> <span class="token class-name">Element</span> e <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在链表尾部添加元素e</span>
<span class="token keyword">void</span> <span class="token function">addFirst</span><span class="token punctuation">(</span> <span class="token class-name">Element</span> e <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在链表头部添加元素e『注意』</span>

<span class="token class-name">Element</span> <span class="token function">removeFirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删除链表头结点中第1个元素</span>
<span class="token class-name">Element</span> <span class="token function">removeLast</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删除链表尾部最后1个元素</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><blockquote><p>Tips：ArrayList和LinkedList都是List类型的子类，做OJ的时候注意</p></blockquote> <h2 id="✅08-优先队列-大顶堆"><a href="#✅08-优先队列-大顶堆" class="header-anchor">#</a> ✅08.优先队列/大顶堆</h2> <ul><li>PriorityQueue 是非线程安全的，所以 Java 提供了 <code>PriorityBlockingQueue</code>（实现 BlockingQueue接口）用于Java 多线程环境。</li></ul> <h3 id="优先队列基础"><a href="#优先队列基础" class="header-anchor">#</a> 优先队列基础</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">PriorityQueue</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Queue</span><span class="token punctuation">;</span>

<span class="token class-name">Queue</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> q <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PriorityQueue</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//注意『前面是</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="🌱java-se-常用类-目录"><a href="#🌱java-se-常用类-目录" class="header-anchor">#</a> 🌱Java SE 常用类-目录</h2> <ul><li>参考：CSDN，<a href="https://blog.csdn.net/weixin_44034328/article/details/103843252" target="_blank" rel="noopener noreferrer">JavaSE常用类<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h3 id="_1-java-lang包"><a href="#_1-java-lang包" class="header-anchor">#</a> 1.<code>java.lang</code>包</h3> <ul><li><p>对象</p></li> <li><p>包装类</p></li> <li><p>数学</p></li> <li><p>字符串</p></li> <li><p>系统类</p></li> <li><p>运行时异常</p></li> <li><p>参考资料：<a href="https://blog.csdn.net/weixin_44034328/article/details/104093664?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-0.control&spm=1001.2101.3001.4242" target="_blank" rel="noopener noreferrer">CSDN的java.lang包<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li></ul> <h3 id="_3-java-util包"><a href="#_3-java-util包" class="header-anchor">#</a> 3.<code>java.util</code>包</h3> <ul><li>哈希表</li> <li>可变数组</li> <li>链表</li> <li>日期</li> <li>数组</li> <li>TreeMap + TreeSet</li></ul> <blockquote><p>查看：Java8的<a href="https://www.matools.com/api/java8" target="_blank" rel="noopener noreferrer">中文文档发现<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <div class="language-txt line-numbers-mode"><pre class="language-text"><code># Interfaces
Collection
Comparator
Deque
Enumeration
EventListener
Formattable
Iterator
List
ListIterator
Map
Map.Entry
NavigableMap
NavigableSet
Observer
PrimitiveIterator
PrimitiveIterator.OfDouble
PrimitiveIterator.OfInt
PrimitiveIterator.OfLong
Queue
RandomAccess
Set
SortedMap
SortedSet


# Classes
AbstractCollection
AbstractList
AbstractMap
AbstractMap.SimpleEntry
AbstractMap.SimpleImmutableEntry
AbstractQueue
AbstractSequentialList
AbstractSet
ArrayDeque
ArrayList
Arrays
Base64
Base64.Decoder
Base64.Encoder
BitSet
Calendar
Calendar.Builder
Collections
Currency
Date
Dictionary
DoubleSummaryStatistics
EnumMap
EnumSet
EventListenerProxy
EventObject
FormattableFlags
Formatter
GregorianCalendar
HashMap
HashSet
Hashtable
IdentityHashMap
IntSummaryStatistics
LinkedHashMap
LinkedHashSet
LinkedList
ListResourceBundle
Locale
Locale.Builder
Locale.LanguageRange
LongSummaryStatistics
Objects
Observable
Optional
OptionalDouble
OptionalInt
OptionalLong
PriorityQueue
Properties
PropertyPermission
PropertyResourceBundle
Random
Scanner
SplittableRandom
Stack
StringJoiner
StringTokenizer
Timer
TimerTask
TimeZone
TreeMap
TreeSet
Vector
WeakHashMap
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br></div></div><ul><li><p><code>java.util.regex</code>包</p> <ul><li>正则表达式</li></ul></li> <li><p><code>java.text</code>包</p> <ul><li>日期格式化</li></ul></li></ul> <h2 id="♻️参考资料"><a href="#♻️参考资料" class="header-anchor">#</a> ♻️参考资料</h2> <ul><li>CyC2018，<a href="https://www.cyc2018.xyz/Java/Java%20%E5%AE%B9%E5%99%A8.html#linkedhashmap" target="_blank" rel="noopener noreferrer">Java部分<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li> <li>labuladong的算法小抄</li> <li>java核心技术36讲，极客时间，杨晓峰</li> <li><a href="https://www.liaoxuefeng.com/wiki/1252599548343744/1265120632401152" target="_blank" rel="noopener noreferrer">java的泛型、集合<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>，廖雪峰</li></ul></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/6.8c2ea393.js" defer></script>
  </body>
</html>
